<?php
/**
 * SellerGroupController.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-05-24 18:57:56
 * @modified   2023-05-24 18:57:56
 */

namespace Plugin\MultiSeller\Http\Controllers\Admin;

use Beike\Admin\Http\Controllers\Controller;
use Beike\Seller\Models\SellerGroup;
use Beike\Seller\Repositories\SellerGroupRepo;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SellerGroupController extends Controller
{
    /**
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        $sellerGroups = SellerGroupRepo::list();
        $data         = [
            'locales'       => locales(),
            'seller_groups' => $sellerGroups,
        ];

        return view('MultiSeller::admin.seller_groups.index', $data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function store(Request $request)
    {
        $requestData = $request->all();

        $seller = SellerGroupRepo::create($requestData);

        return redirect(admin_route('seller_groups.index'))->with('success', trans('common.created_success'));
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function create()
    {
        $sellerGroup  = new SellerGroup();
        $data         = [
            'seller_group'        => $sellerGroup,
        ];

        return view('MultiSeller::admin.seller_groups.form', $data);
    }

    /**
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function edit(int $id)
    {
        $sellerGroup  = SellerGroupRepo::find($id);
        $data         = [
            'seller_group'        => $sellerGroup,
        ];

        return view('MultiSeller::admin.seller_groups.form', $data);
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function update(Request $request, int $id)
    {
        $seller = SellerGroupRepo::update($id, $request->all());

        return redirect(admin_route('seller_groups.index'))->with('success', trans('common.updated_success'));
    }


    /**
     * @param Request $request
     * @param int     $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function updateFeeSale(Request $request, int $id)
    {
        SellerGroupRepo::update($id, $request->only('fee_sale_flat', 'fee_sale_percent'));

        return json_success(trans('common.updated_success'));
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return array
     */
    public function destroy(Request $request, int $id)
    {
        SellerGroupRepo::delete($id);

        return json_success(trans('common.deleted_success'));
    }
}
